package com.andy.blog.admin.service.impl;

import com.andy.blog.admin.dto.SysMenuDTO;
import com.andy.blog.admin.service.SysMenuBusService;
import com.andy.blog.admin.service.SysPermissionBusService;
import com.andy.blog.common.core.domain.entity.SysRole;
import com.andy.blog.common.core.domain.entity.SysUser;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.dal.domain.vo.RouterVo;
import com.andy.blog.dal.service.SysRoleDalService;
import com.andy.blog.dal.service.SysMenuDalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by andy on 2023-04-15 15:28
 */
@Service("sysPermissionBusService")
public class SysPermissionBusServiceImpl implements SysPermissionBusService {

    @Autowired
    private SysRoleDalService sysRoleDalService;

    @Autowired
    private SysMenuDalService menuService;

    @Resource
    private SysMenuBusService sysMenuBusService;

    @Override
    public Set<String> getRolePermission(SysUser user) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(sysRoleDalService.selectRolePermissionByUserId(user.getId()));
        }
        return roles;
    }

    @Override
    public Map<String, Object> getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = this.getRolePermission(user);
        // 权限集合
        Set<String> permissions = this.getMenuPermission(user);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @Override
    public List<RouterVo> getRouters() {
        List<SysMenuDTO> menus = sysMenuBusService.selectMenuTreeByUserId(SecurityUtils.getUserId());
        return sysMenuBusService.buildMenus(menus);
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            perms.add("*:*:*");
        } else {
            List<SysRole> roles = user.getRoles();
            if (!roles.isEmpty() && roles.size() > 1) {
                // 多角色设置permissions属性，以便数据权限匹配权限
                for (SysRole role : roles) {
                    Set<String> rolePerms = menuService.selectMenuPermsByRoleId(role.getId());
                    role.setPermissions(rolePerms);
                    perms.addAll(rolePerms);
                }
            } else {
                perms.addAll(menuService.selectMenuPermsByUserId(user.getId()));
            }
        }
        return perms;
    }
}
